﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using Dictionary.ERP;
using Common;
using Extra.ERP;
using Security.ERP;
using Common.Class;

namespace Dictionary
{
    public class xucRankAdd : xucBaseAdd
    {
        public delegate void SuccessEventHander(object sender, DIC_SALARY_RANK item);

        private readonly IContainer components = null;
        private LabelControl lcDescription;
        private TextEdit txtDescription;

        public xucRankAdd()
        {
            InitializeComponent();
        }

        public event SuccessEventHander Success;

        protected override void Add()
        {
            base.Add();
            base.txtID.Text = new DIC_SALARY_RANK().NewID();
            base.txtNAME.Focus();
        }

        public void Clear()
        {
            base.txtID.Text = "";
            base.txtNAME.Text = "";
            txtDescription.Text = "";
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        protected override void Init()
        {
        }

        private void InitializeComponent()
        {
            var manager = new ComponentResourceManager(typeof (xucRankAdd));
            txtDescription = new TextEdit();
            lcDescription = new LabelControl();
            base.gcInfo.BeginInit();
            base.gcInfo.SuspendLayout();
            base.txtNAME.Properties.BeginInit();
            base.txtID.Properties.BeginInit();
            base.imageCollection2.BeginInit();
            ((ISupportInitialize) base.Err).BeginInit();
            txtDescription.Properties.BeginInit();
            base.SuspendLayout();
            base.gcInfo.Controls.Add(lcDescription);
            base.gcInfo.Controls.Add(txtDescription);
            base.gcInfo.Size = new Size(0x14c, 0x7e);
            base.gcInfo.Controls.SetChildIndex(txtDescription, 0);
            base.gcInfo.Controls.SetChildIndex(lcDescription, 0);
            base.gcInfo.Controls.SetChildIndex(base.lblId, 0);
            base.gcInfo.Controls.SetChildIndex(base.lblName, 0);
            base.gcInfo.Controls.SetChildIndex(base.txtID, 0);
            base.gcInfo.Controls.SetChildIndex(base.txtNAME, 0);
            base.txtNAME.Location = new Point(0x5b, 0x3f);
            base.txtNAME.Size = new Size(0xe7, 20);
            base.lblName.Location = new Point(5, 0x42);
            base.lblName.Size = new Size(0x54, 13);
            base.lblName.Text = "T\x00ean ngạch lương:";
            base.txtID.Location = new Point(0x5b, 0x22);
            base.txtID.Size = new Size(0xe8, 20);
            base.lblId.Location = new Point(5, 0x23);
            base.lblId.Size = new Size(80, 13);
            base.lblId.Text = "M\x00e3 ngạch lương:";
            base.imageCollection2.ImageStream =
                (ImageCollectionStreamer) manager.GetObject("imageCollection2.ImageStream");
            base.btnSave.Location = new Point(0x25, 0x88);
            base.btnCancel.Location = new Point(0xe4, 0x88);
            base.btnSaveNew.Location = new Point(0x87, 0x88);
            txtDescription.Location = new Point(0x5b, 0x5c);
            txtDescription.Name = "txtDescription";
            txtDescription.Size = new Size(0xe7, 20);
            txtDescription.TabIndex = 4;
            lcDescription.Location = new Point(7, 0x5f);
            lcDescription.Name = "lcDescription";
            lcDescription.Size = new Size(0x27, 13);
            lcDescription.TabIndex = 5;
            lcDescription.Text = "Ghi ch\x00fa:";
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Name = "xucRankAdd";
            base.Size = new Size(0x14d, 0xb0);
            base.gcInfo.EndInit();
            base.gcInfo.ResumeLayout(false);
            base.gcInfo.PerformLayout();
            base.txtNAME.Properties.EndInit();
            base.txtID.Properties.EndInit();
            base.imageCollection2.EndInit();
            ((ISupportInitialize) base.Err).EndInit();
            txtDescription.Properties.EndInit();
            base.ResumeLayout(false);
        }

        private void RaiseSuccessEventHander(DIC_SALARY_RANK item)
        {
            if (Success != null)
            {
                Success(this, item);
            }
        }

        public void SetData(DIC_SALARY_RANK item)
        {
            base.txtID.Text = item.RankCode;
            SYS_LOG.Insert("Danh Mục Ngạch Lương", "Xem", base.txtID.Text);
            if (base.m_Status == Actions.Update)
            {
                base.txtID.Properties.ReadOnly = true;
            }
            base.txtNAME.Text = item.RankName;
            txtDescription.Text = item.Description;
        }

        protected override void txtID_EditValueChanged(object sender, EventArgs e)
        {
            var control = (TextEdit) sender;
            if (control.ErrorText != string.Empty)
            {
                base.Err.SetError(control, string.Empty);
            }
            if (base.m_Status == Actions.Add)
            {
                var dic_salary_rank = new DIC_SALARY_RANK();
                if (dic_salary_rank.Exist(control.Text))
                {
                    base.Err.SetError(control, "M\x00e3 đ\x00e3 tồn tại.");
                    control.Focus();
                }
            }
        }

        protected override void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            var control = (TextEdit) sender;
            if ((e.KeyCode == Keys.Enter) | (e.KeyCode == Keys.Tab))
            {
                var dic_salary_rank = new DIC_SALARY_RANK();
                if ((base.m_Status == Common.Class.Actions.Add) && dic_salary_rank.Exist(control.Text))
                {
                    base.Err.SetError(control, "M\x00e3 đ\x00e3 tồn tại.");
                    control.Focus();
                }
            }
        }

        protected override string uc_Change()
        {
            return string.Empty;
        }

        protected override string uc_Delete()
        {
            var item = new DIC_SALARY_RANK
            {
                RankCode = base.txtID.Text
            };
            string str = item.Delete();
            if (str == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            return str;
        }

        protected override string uc_Save()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiRank") != "OK")
            {
                return "";
            }
            if (!MyRule.AllowAdd)
            {
                MyRule.Notify();
                return "";
            }
            SYS_LOG.Insert("Danh Mục Ngạch Lương", "Th\x00eam", base.txtID.Text);
            base.SetWaitDialogCaption("Đang lưu dữ liệu...");
            Cursor.Current = Cursors.WaitCursor;
            var item = new DIC_SALARY_RANK(base.txtID.Text, base.txtNAME.Text, txtDescription.Text);
            string text = item.Insert();
            if (text == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            Cursor.Current = Cursors.Default;
            this.DoHide();
            if (text != "OK")
            {
                XtraMessageBox.Show(text, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            return text;
        }

        protected override string uc_Update()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiRank") != "OK")
            {
                return "";
            }
            if (!MyRule.AllowEdit)
            {
                MyRule.Notify();
                return "";
            }
            SYS_LOG.Insert("Danh Mục Ngạch Lương", "Cập Nhật", base.txtID.Text);
            base.SetWaitDialogCaption("Đang cập nhật dữ liệu...");
            var item = new DIC_SALARY_RANK(base.txtID.Text, base.txtNAME.Text, txtDescription.Text);
            string text = item.Update();
            if (text == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            if (text != "OK")
            {
                XtraMessageBox.Show(text, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            this.DoHide();
            return text;
        }
    }
}